package com.bwie.service.Impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Type;
import com.bwie.mapper.TypeMapper;
import com.bwie.service.TypeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService{
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private FastFileStorageClient fileStorageClient;
    @Override
    public R getTypeTree() {
        List<Type> types = typeMapper.selectList(null);
        List<Tree<String>> build = TreeUtil.build(types, "0", (type, node) -> {
            node.setId(type.getTypeId().toString());
            node.setParentId(type.getTypePid().toString());
            node.setName(type.getTypeName().toString());
            node.putExtra("typeStatus", type.getTypeStatus());
            node.putExtra("typeImage", type.getTypeImage());
            node.putExtra("typeTop", type.getTypeTop());
        });
        return R.OK(build);
    }

    @Override
    public R addTypeList(Type type) {
        Type type1 = typeMapper.selectOne(
                new QueryWrapper<Type>().lambda().eq(Type::getTypeName, type.getTypeName())
        );
        if(ObjectUtils.isNotEmpty(type1)){
            return R.ERROR(500,"名称已存在");
        }
        typeMapper.insert(type);
        return R.OK();
    }

    @Override
    public R uploadTypeImage(MultipartFile file) {
        try {
            String s = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), s, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ERROR(500,"上传失败");
    }

    @Override
    public R getSelectTypeTree() {
        List<Type> types = typeMapper.selectList(null);
        List<Tree<String>> build = TreeUtil.build(types, "0", (type, node) -> {
            node.setId(type.getTypeId().toString());
            node.setParentId(type.getTypePid().toString());
            node.setName(type.getTypeName().toString());
            node.putExtra("label", type.getTypeName());
            node.putExtra("value", type.getTypeId().toString());
        });
        return R.OK(build);
    }
}
